<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Maven多模块项目的依赖管理 | Java技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4, h5 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            margin-top: 1.5em;
            margin-bottom: 0.5em;
        }
        .hero {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .highlight {
            position: relative;
        }
        .highlight:before {
            content: '';
            position: absolute;
            left: -4px;
            top: 0;
            height: 100%;
            width: 4px;
            background: linear-gradient(to bottom, #4f46e5, #7c3aed);
        }
        .drop-cap:first-letter {
            float: left;
            font-size: 3.5em;
            line-height: 0.8;
            margin-right: 0.1em;
            margin-top: 0.1em;
            color: #4f46e5;
            font-weight: bold;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6">Maven多模块项目的依赖管理</h1>
                    <p class="text-xl mb-8 opacity-90">架构设计的"黄金中间地带"</p>
                    <div class="bg-white bg-opacity-20 p-6 rounded-lg mb-8">
                        <p class="text-lg italic">"当你的团队花两个月拆分微服务，却发现性能下降30%，部署复杂度暴增10倍时，你是否扪心自问：真的需要微服务吗？"</p>
                    </div>
                    <a href="#content" class="inline-block bg-white text-indigo-600 font-semibold px-6 py-3 rounded-full hover:bg-gray-100 transition duration-300">
                        <i class="fas fa-book-open mr-2"></i>开始阅读
                    </a>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <img src="https://cdn.nlark.com/yuque/0/2025/png/21449790/1746338235940-644fd17b-4c30-42d3-91e2-3994ddef54b1.png" alt="Maven多模块架构" class="rounded-lg shadow-xl w-full max-w-md">
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main id="content" class="container mx-auto max-w-5xl px-4 md:px-0 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white p-8 rounded-xl shadow-md card">
                <h2 class="text-3xl font-bold mb-6 text-indigo-700"><i class="fas fa-lightbulb text-yellow-400 mr-3"></i>为什么选择Maven多模块？</h2>
                <p class="text-lg mb-6">很多架构师盲目追求"高大上"的微服务，却忽视了Maven多模块这个"黄金中间地带"的价值。我见过太多团队在依赖地狱中挣扎，循环依赖、版本冲突甚至安全漏洞，都源于对Maven依赖管理的误解。</p>
                
                <div class="grid md:grid-cols-3 gap-6 mt-8">
                    <div class="bg-indigo-50 p-6 rounded-lg">
                        <div class="text-indigo-600 text-3xl mb-3"><i class="fas fa-balance-scale"></i></div>
                        <h3 class="font-bold text-lg mb-2">平衡之道</h3>
                        <p>在单体架构与微服务架构之间找到完美平衡点</p>
                    </div>
                    <div class="bg-purple-50 p-6 rounded-lg">
                        <div class="text-purple-600 text-3xl mb-3"><i class="fas fa-project-diagram"></i></div>
                        <h3 class="font-bold text-lg mb-2">模块化设计</h3>
                        <p>保持代码高内聚的同时实现功能解耦</p>
                    </div>
                    <div class="bg-blue-50 p-6 rounded-lg">
                        <div class="text-blue-600 text-3xl mb-3"><i class="fas fa-rocket"></i></div>
                        <h3 class="font-bold text-lg mb-2">高效演进</h3>
                        <p>为未来向微服务演进奠定坚实基础</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Chapter 1 -->
        <section class="mb-16">
            <div class="bg-white p-8 rounded-xl shadow-md card">
                <h2 class="text-2xl font-bold mb-4 text-indigo-700 border-b pb-2">一、多模块项目的核心价值</h2>
                
                <h3 class="text-xl font-semibold mt-6 mb-3 flex items-center">
                    <span class="bg-indigo-600 text-white rounded-full w-6 h-6 flex items-center justify-center mr-2 text-sm">1</span>
                    多模块项目的定位
                </h3>
                <p class="mb-4 drop-cap">Maven多模块项目是在单体架构与微服务架构之间的绝佳平衡点。它允许我们在保持代码高内聚的同时，实现逻辑分层和功能解耦。多模块项目本质上是一种"内部拆分"的方式，通过模块化设计使复杂系统变得可管理，同时避免了微服务带来的分布式复杂性。</p>
                
                <div class="bg-gray-100 p-4 rounded-lg my-6">
                    <h4 class="font-semibold mb-2 flex items-center">
                        <i class="fas fa-code text-indigo-600 mr-2"></i>典型多模块项目结构
                    </h4>
                    <pre class="bg-gray-800 text-gray-100 p-4 rounded overflow-x-auto"><code>project-parent
├── pom.xml
├── common-module
│   └── pom.xml
├── user-service
│   └── pom.xml
├── order-service
│   └── pom.xml
└── payment-service
    └── pom.xml</code></pre>
                </div>
                
                <h3 class="text-xl font-semibold mt-8 mb-3 flex items-center">
                    <span class="bg-indigo-600 text-white rounded-full w-6 h-6 flex items-center justify-center mr-2 text-sm">2</span>
                    多模块项目解决的核心问题
                </h3>
                <ul class="list-disc pl-6 mb-6 space-y-2">
                    <li><span class="font-semibold">代码复用</span>：共享基础设施和通用组件，减少重复代码</li>
                    <li><span class="font-semibold">团队协作</span>：不同团队可以专注于不同模块的开发</li>
                    <li><span class="font-semibold">依赖隔离</span>：合理划分模块边界，避免过度耦合</li>
                    <li><span class="font-semibold">按需部署</span>：支持模块级别的独立构建和部署</li>
                    <li><span class="font-semibold">渐进式演进</span>：为未来向微服务演进奠定基础</li>
                </ul>
                
                <h3 class="text-xl font-semibold mt-8 mb-3 flex items-center">
                    <span class="bg-indigo-600 text-white rounded-full w-6 h-6 flex items-center justify-center mr-2 text-sm">3</span>
                    大厂实践案例分析
                </h3>
                <div class="bg-blue-50 p-6 rounded-lg">
                    <p class="mb-4">阿里巴巴在其电商平台早期采用了多模块Maven架构，将系统分为用户中心、商品中心、订单中心等核心模块。通过这种方式，他们在保持系统一致性的同时，实现了业务领域的解耦和独立演进。</p>
                    <p class="font-semibold text-blue-700">这种架构在支撑每年"双11"高并发场景时，展现出了强大的可扩展性。</p>
                </div>
            </div>
        </section>

        <!-- Chapter 2 -->
        <section class="mb-16">
            <div class="bg-white p-8 rounded-xl shadow-md card">
                <h2 class="text-2xl font-bold mb-4 text-indigo-700 border-b pb-2">二、Maven依赖管理的基础机制</h2>
                
                <h3 class="text-xl font-semibold mt-6 mb-3 flex items-center">
                    <span class="bg-indigo-600 text-white rounded-full w-6 h-6 flex items-center justify-center mr-2 text-sm">1</span>
                    依赖传递原理详解
                </h3>
                <p class="mb-4">Maven的依赖传递是指：当A依赖B，B依赖C，那么A会自动依赖C。这种机制大大简化了依赖管理，但也带来了依赖冲突的可能性。</p>
                
                <div class="bg-yellow-50 p-4 rounded-lg my-4">
                    <h4 class="font-semibold mb-2 flex items-center">
                        <i class="fas fa-exclamation-triangle text-yellow-600 mr-2"></i>依赖冲突示例
                    </h4>
                    <p class="mb-2">模块A依赖spring-core 5.3.9，而模块B依赖spring-context 5.2.6（间接依赖spring-core 5.2.6），这时项目中就会出现两个不同版本的spring-core。</p>
                    <pre class="bg-gray-800 text-gray-100 p-4 rounded overflow-x-auto"><code>&lt;!-- 模块A的依赖 --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
    &lt;version&gt;5.3.9&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- 模块B的依赖 --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
    &lt;version&gt;5.2.6&lt;/version&gt;
&lt;/dependency&gt;</code></pre>
                </div>
                
                <!-- Mermaid Diagram -->
                <div class="my-8">
                    <div class="mermaid">
                        graph TD
                            A[模块A] --> B[spring-core 5.3.9]
                            B --> C[其他依赖]
                            D[模块B] --> E[spring-context 5.2.6]
                            E --> F[spring-core 5.2.6]
                    </div>
                </div>
                
                <h3 class="text-xl font-semibold mt-8 mb-3 flex items-center">
                    <span class="bg-indigo-600 text-white rounded-full w-6 h-6 flex items-center justify-center mr-2 text-sm">2</span>
                    依赖冲突的产生与解决
                </h3>
                <p class="mb-4">Maven解决依赖冲突主要遵循两个原则：</p>
                <ul class="list-disc pl-6 mb-6 space-y-2">
                    <li><span class="font-semibold">最短路径优先</span>：越直接的依赖越优先</li>
                    <li><span class="font-semibold">声明顺序优先</span>：POM文件中先声明的依赖优先</li>
                </ul>
                
                <div class="grid md:grid-cols-2 gap-6 mt-6">
                    <div class="bg-green-50 p-4 rounded-lg">
                        <h4 class="font-semibold mb-2 flex items-center">
                            <i class="fas fa-search text-green-600 mr-2"></i>查看依赖树
                        </h4>
                        <pre class="bg-gray-800 text-gray-100 p-4 rounded overflow-x-auto"><code>mvn dependency:tree -Dverbose</code></pre>
                    </div>
                    <div class="bg-purple-50 p-4 rounded-lg">
                        <h4 class="font-semibold mb-2 flex items-center">
                            <i class="fas fa-tools text-purple-600 mr-2"></i>解决冲突的方法
                        </h4>
                        <pre class="bg-gray-800 text-gray-100 p-4 rounded overflow-x-auto"><code>&lt;!-- 方法1：在父POM中使用dependencyManagement统一版本 --&gt;
&lt;dependencyManagement&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
            &lt;version&gt;5.3.9&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/dependencyManagement&gt;

&lt;!-- 方法2：排除传递依赖 --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
    &lt;version&gt;5.2.6&lt;/version&gt;
    &lt;exclusions&gt;
        &lt;exclusion&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
        &lt;/exclusion&gt;
    &lt;/exclusions&gt;
&lt;/dependency&gt;</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Chapter 3 -->
        <section class="mb-16">
            <div class="bg-white p-8 rounded-xl shadow-md card">
                <h2 class="text-2xl font-bold mb-4 text-indigo-700 border-b pb-2">三、父子工程的POM配置</h2>
                
                <h3 class="text-xl font-semibold mt-6 mb-3 flex items-center">
                    <span class="bg-indigo-600 text-white rounded-full w-6 h-6 flex items-center justify-center mr-2 text-sm">1</span>
                    继承与聚合的区别与应用场景
                </h3>
                <p class="mb-4">Maven提供了两种组织多模块项目的机制：</p>
                <ul class="list-disc pl-6 mb-6 space-y-2">
                    <li><span class="font-semibold">继承</span>：子模块继承父模块的配置（通过<code>&lt;parent&gt;</code>标签）</li>
                    <li><span class="font-semibold">聚合</span>：父模块包含子模块（通过<code>&lt;modules&gt;</code>标签）</li>
                </ul>
                
                <div class="grid md:grid-cols-2 gap-6 mt-6">
                    <div class="bg-blue-50 p-4 rounded-lg">
                        <h4 class="font-semibold mb-2 flex items-center">
                            <i class="fas fa-sitemap text-blue-600 mr-2"></i>父POM配置
                        </h4>
                        <pre class="bg-gray-800 text-gray-100 p-4 rounded overflow-x-auto"><code>&lt;groupId&gt;com.company&lt;/groupId&gt;
&lt;artifactId&gt;parent-project&lt;/artifactId&gt;
&lt;version&gt;1.0.0&lt;/version&gt;
&lt;packaging&gt;pom&lt;/packaging&gt; &lt;!-- 关键：必须是pom类型 --&gt;

&lt;modules&gt;
    &lt;module&gt;common-module&lt;/module&gt;
    &lt;module&gt;user-service&lt;/module&gt;
    &lt;module&gt;order-service&lt;/module&gt;
&lt;/modules&gt;</code></pre>
                    </div>
                    <div class="bg-green-50 p-4 rounded-lg">
                        <h4 class="font-semibold mb-2 flex items-center">
                            <i class="fas fa-code-branch text-green-600 mr-2"></i>子POM配置
                        </h4>
                        <pre class="bg-gray-800 text-gray-100 p-4 rounded overflow-x-auto"><code>&lt;parent&gt;
    &lt;groupId&gt;com.company&lt;/groupId&gt;
    &lt;artifactId&gt;parent-project&lt;/artifactId&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
&lt;/parent&gt;
&lt;artifactId&gt;user-service&lt;/artifactId&gt;</code></pre>
                    </div>
                </div>
                
                <h3 class="text-xl font-semibold mt-8 mb-3 flex items-center">
                    <span class="bg-indigo-600 text-white rounded-full w-6 h-6 flex items-center justify-center mr-2 text-sm">2</span>
                    dependencyManagement与pluginManagement深度解析
                </h3>
                <p class="mb-4">这两个机制是Maven多模块项目的核心，它们允许在父POM中声明依赖和插件的版本，而不实际引入它们：</p>
                
                <div class="bg-gray-100 p-4 rounded-lg my-6">
                    <pre class="bg-gray-800 text-gray-100 p-4 rounded overflow-x-auto"><code>&lt;dependencyManagement&gt;
    &lt;dependencies&gt;
        &lt;!-- 声明依赖版本，但不实际引入 --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-dependencies&lt;/artifactId&gt;
            &lt;version&gt;2.6.7&lt;/version&gt;
            &lt;type&gt;pom&lt;/type&gt;
            &lt;scope&gt;import&lt;/scope&gt;
        &lt;/dependency&gt;
        
        &lt;dependency&gt;
            &lt;groupId&gt;com.alibaba&lt;/groupId&gt;
            &lt;artifactId&gt;fastjson&lt;/artifactId&gt;
            &lt;version&gt;1.2.83&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/dependencyManagement&gt;

&lt;pluginManagement&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.8.1&lt;/version&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/pluginManagement&gt;</code></pre>
                </div>
            </div>
        </section>

        <!-- Key Takeaways -->
        <section class="mb-16">
            <div class="bg-indigo-700 text-white p-8 rounded-xl shadow-lg">
                <h2 class="text-2xl font-bold mb-6 flex items-center">
                    <i class="fas fa-star mr-3"></i>关键结论
                </h2>
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="border-l-4 border-yellow-400 pl-4">
                        <h3 class="text-xl font-semibold mb-2">微服务 vs 多模块</h3>
                        <p>不要盲目追求微服务架构，评估你的团队规模和项目复杂度。Maven多模块可能是更适合的解决方案。</p>
                    </div>
                    <div class="border-l-4 border-green-400 pl-4">
                        <h3 class="text-xl font-semibold mb-2">依赖管理原则</h3>
                        <p>合理使用dependencyManagement和pluginManagement，统一管理依赖版本，避免冲突。</p>
                    </div>
                    <div class="border-l-4 border-blue-400 pl-4">
                        <h3 class="text-xl font-semibold mb-2">模块划分</h3>
                        <p>按业务领域或技术层次划分模块，保持高内聚低耦合，为未来演进留出空间。</p>
                    </div>
                    <div class="border-l-4 border-purple-400 pl-4">
                        <h3 class="text-xl font-semibold mb-2">持续优化</h3>
                        <p>定期检查依赖树，清理无用依赖，更新有安全漏洞的依赖版本。</p>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-4 md:px-0">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">Java技术小馆</h3>
                    <p class="text-sm">专业技术分享平台</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-indigo-300 hover:text-white transition duration-300">
                        <i class="fas fa-external-link-alt mr-2"></i>访问技术小馆
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-sm">
                <p>© 2023 Java技术小馆. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>